//
// StoreNuke.Net -  http://www.storenuke.net
// Copyright (c) 2005
// by Robert J Collins ( info@storenuke.net ) of WillowTree Software, Inc. ( http://www.willowtreesoftware.com )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using StoreNuke.StarterKit.Commerce.Components;

namespace StoreNuke.StarterKit.Commerce
{
	/// <summary>
	///		Summary description for Skin.
	/// </summary>
	public class Skin : UserControl
	{
		protected System.Web.UI.HtmlControls.HtmlTableCell LeftPane;
		protected PlaceHolder SkinPlaceHolder;
//		public string description = "";
//		public string keyWords = "";
//		public string copyright = "";
		private void Page_Load(object sender, EventArgs e)
		{
			//Create DB Istances
			StoreControlsDB storeDB = new StoreControlsDB();
			StoreGlobalDB storeInfo = new StoreGlobalDB();
			//Create DataReader
			SqlDataReader drStoreInfo = storeInfo.GetGeneralAdministration();
			//StoreTitle
			string StoreName = "";
			// lookup "as" in the C# language reference if you don't understand it already
			CommerceDefault parentPage = this.Page as CommerceDefault; 
			if (null != parentPage){
				parentPage.generator = "StoreNuke.Net";
				parentPage.author = "StoreNuke.Net";
				if(drStoreInfo.Read())
				{
					parentPage.copyright = drStoreInfo["Copyright"].ToString();
					parentPage.description = drStoreInfo["DefaultMetaDesc"].ToString();
					parentPage.keyWords = drStoreInfo["DefaultMetaKey"].ToString();
					StoreName = drStoreInfo["DefaultPageTitle"].ToString() + " > ";
				}
				drStoreInfo.Close();
				SqlDataReader dr = storeDB.GetStoreControlInfo(Convert.ToInt32(Request.QueryString["scid"] != null ? Convert.ToInt32(Request.QueryString["scid"]) : 1));
				if(dr.Read())
				{
					parentPage.title = StoreName + dr["StoreControlName"].ToString();
				}
				else if(StoreName != "")
				{
					parentPage.title = StoreName + "Shopping";
				}
				dr.Close();
			}
		}

		public bool IsStoreAdmin
		{
			get{return Security.IsStoreAdmin(Context.User.Identity.Name);}
		}

		private UserControl LoadSkin(string SkinPath) 
		{ 
			UserControl ctlSkin = new UserControl(); 
			string error;

			try 
			{ 
				ctlSkin = ((UserControl)(LoadControl(SkinPath))); 
			} 
			catch
			{
				//If there is an error in the skin user control
				//The system will load the default.
				//The defalt is on step in from the others so we 
				//have to change the path by removing the extra ../
				try
				{
					ctlSkin = ((UserControl)(LoadControl(SkinPath.Substring(SkinPath.LastIndexOf("../")))));
				}
				catch (Exception ex) 
				{ 
					error = "<center>Could Not Load Skin: " + SkinPath + " Error: " + Server.HtmlEncode(ex.Message) + "</center><br>"; 
					//SkinError.Visible = true; 
				} 
			}
			return ctlSkin; 
		}

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
			
			//load skin control
			UserControl ctlSkin = new UserControl();

			//Create DB Istance
			StoreControlsDB storeDB = new StoreControlsDB();

			//Load StoreControl "Selected Skin Path"
			if(Request.QueryString["scid"]!= null)
			{
				if(Request.QueryString["sctl"] == null)
					ctlSkin = LoadSkin(storeDB.GetStoreControl(Convert.ToInt32(Request.QueryString["scid"])));
				else if(!IsStoreAdmin){
						Response.Redirect("Default.aspx?scid=14");
				}
				else
				{
					ctlSkin = LoadSkin(storeDB.GetStoreControlAdmin(Convert.ToInt32(Request.QueryString["scid"])));
				}
			}
			else
			{
				if(Request.QueryString["sctl"] == null)
					ctlSkin = LoadSkin(storeDB.GetStoreControl(1));
				else if(!IsStoreAdmin)
				{
					Response.Redirect("Default.aspx?scid=14");
				}
				else
				{
					ctlSkin = LoadSkin(storeDB.GetStoreControlAdmin(1));
				}
			}

			//Load default StoreControl if error ocurs
			if (ctlSkin == null)
			{
				ctlSkin = LoadSkin(ConfigurationSettings.AppSettings["defaultStoreControl"]);
			}

			//Add skin to page
			SkinPlaceHolder.Controls.Add(ctlSkin);
		}
		
		/// <summary>
		///		Required method for Designer support - do not modify
		///		the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion
	}
}
